package com.nq.service.impl;

/**
 * 开发者----wwx
 * 联系TG：gncms
 */

import com.nq.common.ServerResponse;
import com.nq.dao.UserUsdtMapper;
import com.nq.pojo.User;
import com.nq.pojo.UserBank;
import com.nq.pojo.UserUsdt;
import com.nq.service.IUserService;
import com.nq.service.IUserUsdtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Service("iUserUsdtService")
public class UserUsdtServiceImpl implements IUserUsdtService {

  @Autowired
  private UserUsdtMapper userUsdtMapper;

  @Autowired
  private IUserService userService;

  @Override
  public ServerResponse addUsdt(UserUsdt userUsdt, HttpServletRequest request) {
    User user = this.userService.getCurrentUser(request);
    if (user == null ){
      return ServerResponse.createBySuccessMsg("Please log in first.");
    }

    UserUsdt usdt = this.userUsdtMapper.findUserUsdtByUserId(user.getId());
    if (null != usdt) {
      userUsdt.setNetType(usdt.getNetType());
      userUsdt.setUsdtAddr(usdt.getUsdtAddr());
      this.userUsdtMapper.updateByPrimaryKeySelective(userUsdt);
      return ServerResponse.createBySuccess("Modify usdt successfully");
    }

    userUsdt.setUserId(user.getId());
    userUsdt.setCreateTime(new Date());
    int count = this.userUsdtMapper.insertSelective(userUsdt);
    if (count > 0) {
      return ServerResponse.createBySuccess("Added usdt successfully");
    }
    return ServerResponse.createByErrorMsg("Failed to add usdt");
  }


  public ServerResponse updateUsdt(UserUsdt usdt, HttpServletRequest request) {
    User user = this.userService.getCurrentUser(request);

    UserUsdt userUsdt = this.userUsdtMapper.findUserUsdtByUserId(user.getId());
    if (userUsdt == null) {
      return ServerResponse.createByErrorMsg("The modification failed and the usdt information could not be found.");
    }
    userUsdt.setNetType(usdt.getNetType());
    userUsdt.setUsdtAddr(usdt.getUsdtAddr());
    int updateCount = this.userUsdtMapper.updateByPrimaryKeySelective(userUsdt);
    if (updateCount > 0) {
      return ServerResponse.createBySuccess("Modify usdt successfully");
    }
    return ServerResponse.createByErrorMsg("Failed to modify usdt");
  }


  @Override
  public ServerResponse getUsdtInfo(HttpServletRequest request) {
    User user = this.userService.getCurrentUser(request);

    UserUsdt userUsdt = this.userUsdtMapper.findUserUsdtByUserId(user.getId());
    if (userUsdt == null) {
      return ServerResponse.createByErrorMsg("The modification failed, and the usdt information could not be found.");
    }
    return ServerResponse.createBySuccess(userUsdt);
  }

  @Override
  public UserUsdt findUserUsdtByUserId(Integer userId) {
    return this.userUsdtMapper.findUserUsdtByUserId(userId);
  }
}
